﻿using System.Collections.Generic;
using System.Linq;

namespace StoryCardLogic.ImportExportData
{
    /// <summary>
    ///     Подгрузка историй из текстовых файлов
    ///     Одна линия - одна история
    /// </summary>
    public class TxtStoryLoader : IStoryLoader
    {
        private readonly IFile _fileManager;
        private readonly IStoryManager _storyManager;

        public TxtStoryLoader(IFile fileManager, IStoryManager storyManager)
        {
            _fileManager = fileManager;
            _storyManager = storyManager;
        }

        /// <summary>
        ///     Подгрузка текстового файла
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public List<Story> Load(string path)
        {
            string text = _fileManager.ReadFromFile(path);
            string[] splittedText = text.Split('\n');
            _storyManager.Stories = new List<Story>(splittedText.Count());
            foreach (string s in splittedText)
            {
                string trimmedText = s.Trim();
                if (trimmedText.Length > 0)
                    _storyManager.AddStory(trimmedText);
            }
            return _storyManager.Stories;
        }
    }
}